package org.hydrakyoufeng.vv.user;

import org.hydrakyoufeng.vv.Result;
import org.hydrakyoufeng.vv.configuration.TokenCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private TokenCache cache;

    public Result login(UserBean userBean) {
        if (userBean.getMobile() == null
                || userBean.getMobile().length() == 0
                || userBean.getPassword() == null
                || userBean.getPassword().length() == 0) {
            return Result.fail("电话号码或密码不能为空");
        }
        Map<String,Object> row = userDao.queryRow(userBean);
        // 失败
        if (null == row) {
            return Result.fail("用户名或密码错误");
        }
        // 使用雪花算法计算token
        String token = UUID.randomUUID().toString();
        // 缓存token，与用户主键关联
        cache.put(token,row.get("t_user_pk"));
        row.put("token",token);
        return Result.ok(row);
    }

}
